#include<iostream>
#include<string>
#include<vector>
#include<ctime>
#include<algorithm>

using namespace std;

//常用拷贝算法和替换算法
//copy			容器内指定范围的元素拷贝到另一个容器中
//replace		将容器内指定范围的旧元素改为新元素
//replace_if	容器内指定范围满足条件的元素替换为新元素
//swap			互换两个容器的元素



//copy(iterator beg1, iterator end1, iterator beg2)
/*
void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}

	vector<int> v2;
	//v2提前开辟空间
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	return 0;
}
*/



/*
//replace(iterator beg, iterator end, oldvalue, newvalue)
//会将所有的odvalue 替换为newvlaue
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i * 10);
	}

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	replace(v.begin(), v.end(), 20, 666);
	cout << "替换后:" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}


//replace_if(iterator beg, iterator end, _Pred, newvlaue)
class Greater40
{
public:
	bool operator()(int val)
	{
		return val > 40;
	}
};

void test02()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i * 10);
	}

	cout << "替换前:" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;

	replace_if(v.begin(), v.end(), Greater40(), 666);
	cout << "替换后:" << endl;
	for_each(v.begin(), v.end(), MyPrint());
	cout << endl;
}

int main()
{
	test01();
	cout << "---------------------------------" << endl;
	test02();
	return 0;
}
*/





//swap(container c1, container c2)
//c1容器1 和c2容器2是同种类型容器
/*
class MyPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};


void test01()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(10 * i);
	}

	cout << "交换前: " << endl;
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;

	swap(v1, v2);
	cout << "交换后: " << endl;
	for_each(v1.begin(), v1.end(), MyPrint());
	cout << endl;
	for_each(v2.begin(), v2.end(), MyPrint());
	cout << endl;
}

int main()
{
	test01();
	return 0;
}
*/








/*
//常用算数生成算法
//属于是小型算法，头文件#include<numeric>
//accumulate		计算容器元素累计总和
//fill				向容器中添加元素

#include<numeric>

//accumulate(iterator beg, iterator beg, value)
//value是起始累加值，一般为0
void test01()
{
	vector<int> v;
	for (int i = 1; i <= 100; i++)
	{
		v.push_back(i);
	}

	int num = accumulate(v.begin(), v.end(), 0);
	cout << num << endl;
}

//fill(iterator beg, iterator end, value)
//value是填充的值
class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

void test02()
{
	vector<int> v;
	v.resize(10);			//默认为0
	cout << "填充前: " << endl;
	for_each(v.begin(), v.end(), myPrint());
	cout << endl << endl;

	cout << "填充后: " << endl;
	fill(v.begin(), v.end(), 666);
	for_each(v.begin(), v.end(), myPrint());
	cout << endl;
}

int main()
{
	test01();
	test02();
	return 0;
}
*/











//常用集合算法
//set_intersection		求两个容器的交集
//set_union				求两个容器的并集
//set_difference		求两个容器的差集
//使用时，原容器必须是有序数列！！！

//set_intersection(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest)
//dest是目标容器开始迭代器

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int> v3;
	//目标容器需要提前开辟空间
	v3.resize(min(v1.size(), v2.size()));

	//最后返回一个结束迭代器
	vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

	for_each(v3.begin(), itEnd, myPrint);	//一般用这个
	cout << endl;
	for_each(v3.begin(), v3.end(), myPrint);
	cout << endl;
}

//set_union(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator beg3)

void test02()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int> v3;
	//目标容器需要提前开辟空间
	v3.resize(v1.size() + v2.size());

	//最后返回一个结束迭代器
	vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());

	for_each(v3.begin(), itEnd, myPrint);	
	cout << endl;
}

//set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator beg3)
void test03()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 5);
	}

	vector<int> v3;
	vector<int> v4;
	
	//v1和v2的差集
	//目标容器需要提前开辟空间
	v3.resize(v1.size());
	//最后返回一个结束迭代器
	vector<int>::iterator itEnd1 = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), itEnd1, myPrint);
	cout << endl;

	//v2和v1的差集
	v4.resize(v2.size());
	vector<int>::iterator itEnd2 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v4.begin());
	for_each(v4.begin(), itEnd2, myPrint);
	cout << endl;
}

int main()
{
	test01();
	test02();
	test03();
	return 0;
}





